// 笔试强训day27:走迷宫

// 广度优先遍历，以前都是递归做的，其实可以用队列
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main() {
    int n, m, xs, ys, xt, yt;
    cin >> n >> m >> xs >> ys >> xt >> yt;
    vector<string> Map(n);
    for (int i = 0; i < n; i++)
    {
        cin >> Map[i];
    }
    vector<vector<int>> dp(n, vector<int>(m, -1));
    queue<pair<int, int>> q;
    dp[xs - 1][ys - 1] = 0;

    vector<int> cx = {0, 0, -1, 1}, cy = {-1, 1, 0, 0};
    q.push({xs - 1, ys - 1});
    while(!q.empty())
    {
        auto pos = q.front();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            int x = pos.first + cx[i], y = pos.second + cy[i];
            if(x >= 0 && x < n && y >= 0 && y < m && dp[x][y] == -1 && Map[x][y] == '.')
            {
                q.push({x, y});
                dp[x][y] = dp[pos.first][pos.second] + 1;
            }
        }
    }
    cout << dp[xt - 1][yt - 1] << endl;
}